package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class StatementServiceImpl implements IStatementService {
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;
    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }

    @Override
    public void save(Statement statement) {
        //前台传递过来的数据不能直接使用
        Statement newObj = new Statement();
        newObj.setCustomerName(statement.getCustomerName());//客户姓名
        newObj.setCustomerPhone(statement.getCustomerPhone());//客户联系方式
        newObj.setActualArrivalTime(statement.getActualArrivalTime());//实际到店时间
        newObj.setLicensePlate(statement.getLicensePlate());//车牌号码
        newObj.setCarSeries(statement.getCarSeries());//汽车类型
        newObj.setServiceType(statement.getServiceType());//服务类型
        newObj.setCreateTime(new Date());//创建时间
        newObj.setInfo(statement.getInfo());//备注
        statementMapper.insert(newObj);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(String ids) {

    }

    @Override
    public void update(Statement statement) {
        //只有在消费中才可以修改
        Statement oldObj = this.get(statement.getId());
        if (!Statement.STATUS_CONSUME.equals(oldObj.getStatus())){
                throw new BusinessException("已结算订单无法修改");
        }
        oldObj.setCustomerName(statement.getCustomerName());//客户姓名
        oldObj.setCustomerPhone(statement.getCustomerPhone());//客户联系方式
        oldObj.setActualArrivalTime(statement.getActualArrivalTime());//实际到店时间
        oldObj.setLicensePlate(statement.getLicensePlate());//车牌号码
        oldObj.setCarSeries(statement.getCarSeries());//汽车类型
        oldObj.setServiceType(statement.getServiceType());//服务类型
        oldObj.setInfo(statement.getInfo());//备注
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void remove(Long[] ids) {
        if (ids !=null && ids.length>0){
            for (Long id : ids) {
                    statementMapper.updateIsDelById(id,Statement.IS_DELETE);
            }
        }
    }

    @Override
    @Transactional
    public void pay(Long statementId) {
        //只有在消费状态下才能支付
        //根据传入的id查询出当前结算单的信息
        Statement statement = this.get(statementId);
        //判断其状态必须为 消费中 才可以进行支付
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("请不要重复支付");
        }
        //在结算单中记录收款人和收款时间
        statement.setPayee(ShiroUtils.getUser());  //收款人
        statement.setPayTime(new Date());          //收款时间
        //将结算单状态修改为已支付
        statement.setStatus(Statement.STATUS_PAID);
        //修改结算单状态
        statementMapper.updateByPrimaryKey(statement);
        //判断结算单中的预约单id是否存在
        if (statement.getAppointmentId() != null){
            //说明该结算单是预约单生成的,需要将预约单的状态也改为已支付
            appointmentService.changeStatus(statement.getAppointmentId(),Appointment.STATUS_PAID);
        }
    }

    @Override
    public Long generateStatement(Long appointmentId) {
      //获取结算单
     Statement statement = statementMapper.getByAppointmentId(appointmentId);
     //获取预约单
      Appointment appointment = appointmentService.get(appointmentId);
        // 没有通过该预约单 id 查询到对应的结算单。并且 该预约单的状态是已到店
        if (statement == null && Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())){
            //  已到店  生成结算单
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());//客户姓名
            statement.setCustomerPhone(appointment.getCustomerPhone());//客户联系方式
            statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            statement.setCarSeries(appointment.getCarSeries());//汽车类型
            statement.setServiceType(appointment.getServiceType());//服务类型
            statement.setAppointmentId(appointmentId);  //预约单id
            statement.setCreateTime(new Date());//创建时间
            statement.setInfo(statement.getInfo());//备注
            statementMapper.insert(statement);
            //更新预约单状态  已结算
            appointmentService.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
        }
        return statement.getId();
    }
}
